<template>
  <div class="settings-container">
    <!-- 头部区域 -->
    <div class="header">
      <div class="header-left">
        <button class="back-button" @click="goBack">
          <i class="iconfont icon-arrow-left"></i>
        </button>
        <h1 class="title">系统设置</h1>
      </div>
    </div>

    <div class="settings-grid">
      <!-- 首页顶图管理 -->
      <div class="setting-card">
        <div class="card-header">
          <i class="iconfont icon-home"></i>
          <h3>APP首页顶图管理</h3>
        </div>

        <div class="card-body">
          <div class="upload-area">

        <ImageUploader 
          v-model="homeBanner"
          accept="image/jpeg,image/png"
          @upload-success="handleUploadSuccess('home', $event)"
          @update:modelValue="handleImageSelected('home', $event)"
        />
        <div class="upload-tips">建议尺寸：750x300像素，JPG/PNG格式</div>
          </div>
        </div>
      </div>

      <!-- 我的页面顶图管理 -->
      <div class="setting-card">
        <div class="card-header">
          <i class="iconfont icon-user"></i>
          <h3>APP我的页面顶图管理</h3>
        </div>

        <div class="card-body">
          <div class="upload-area">

        <ImageUploader 
          v-model="profileBanner"
          accept="image/jpeg,image/png"
          @upload-success="handleUploadSuccess('profile', $event)"
          @update:modelValue="handleImageSelected('profile', $event)"
        />
        <div class="upload-tips">建议尺寸：750x200像素，JPG/PNG格式</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { vToast } from '@/plugins/vToast';
import { get, post } from '@/request';
import ImageUploader from '@/components/ImageUploader.vue';

const router = useRouter();
const settings = ref({});
const currentIcon = ref("");
const homeBanner = ref("");
const profileBanner = ref("");
const isSaving = ref(false);

// 获取所有设置
const fetchSettings = async () => {
  try {
    const res = await get("/setting/getAll");
    if (res && res.code === 200) {
      settings.value = res.data;
      currentIcon.value = res.data.site_logo || "";
      homeBanner.value = res.data.app_home_banner || "";
      profileBanner.value = res.data.app_profile_banner || "";
    }
  } catch (error) {
    vToast({ message: "获取设置失败" });
    console.error("获取设置失败:", error);
  }
};

// 初始化时获取设置
onMounted(() => {
  fetchSettings();
});

// 更新设置项
const updateSetting = async (key, value) => {
  try {
    isSaving.value = true;
    const res = await post("/setting/update", {
      key: key,
      value: value
    });
    if (res && res.code === 200) {
    } else {
      vToast({ message: res?.msg || "保存失败" });
    }
  } catch (error) {
    vToast({ message: "保存设置失败" });
    console.error("保存设置失败:", error);
  } finally {
    isSaving.value = false;
  }
};

// 处理上传成功
const handleImageSelected = (type, url) => {
  if (!url) return;
  
  switch (type) {
    case "home":
      homeBanner.value = url;
      updateSetting("app_home_banner", url);
      break;
    case "profile":
      profileBanner.value = url;
      updateSetting("app_profile_banner", url);
      break;
  }
};

const handleUploadSuccess = (type, response) => {
  if (response.code !== 200) {
    vToast({ message: response.msg || "上传失败", class: 'error' });
    return;
  }

  vToast({ message: response.msg || "上传成功", class: 'success' });
  const url = response.data?.url;
  if (!url) {
    vToast({ message: "获取上传文件URL失败", class: 'error' });
    return;
  }
  
  handleImageSelected(type, url);
};

const goBack = () => {
  router.go(-1);
};
</script>

<style scoped lang="less">
.settings-container {
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);

  .header-left {
    display: flex;
    align-items: center;

    .title {
      margin: 0;
      font-size: 20px;
      font-weight: 500;
      color: #303133;
    }
  }
}

.back-button {
  width: 32px;
  height: 32px;
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    border-color: var(--bs-theme-color);
    color: var(--bs-theme-color);
  }

  .iconfont {
    font-size: 14px;
  }
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 20px;

  .setting-card {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    padding: 20px;
  }
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;

  i {
    font-size: 24px;
    margin-right: 10px;
    color: var(--bs-theme-color);
  }

  h3 {
    margin: 0;
    font-size: 18px;
  }
}

.upload-area {
  display: flex;
  gap: 30px;
  align-items: center;
}

.current-icon {
  text-align: center;

  .icon-preview {
    width: 120px;
    height: 120px;
    border-radius: 24px;
    object-fit: cover;
    border: 1px solid #eee;
  }

  .banner-preview {
    width: 200px;
    height: 100px;
    border-radius: 8px;
    object-fit: cover;
    border: 1px solid #eee;
  }

  .icon-info {
    margin-top: 10px;
    font-size: 14px;
    color: #666;
  }
}

.upload-controls {
  flex: 1;

  .upload-btn {
    padding: 10px 20px;
    background: var(--bs-theme-color);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;

    &:hover {
      opacity: 0.9;
    }

    .iconfont {
      font-size: 16px;
    }
  }

  .upload-tips {
    margin-top: 10px;
    font-size: 12px;
    color: #999;
  }
}
</style>